package com.zhfi.dataapply.schedule.writer.chaincode;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhfi.dataapply.entity.data.po.chaincode.SocialSecurityProvinceCountPO;
import com.zhfi.dataapply.mapper.chaincode.SocialSecurityProvinceCountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class SocialSecurityProvinceCountWriter implements ItemWriter<SocialSecurityProvinceCountPO> {

    @Autowired
    private SocialSecurityProvinceCountMapper mapper;

    @Override
    public void write(List<? extends SocialSecurityProvinceCountPO> list) throws Exception {
        for (SocialSecurityProvinceCountPO po : list) {
            try {
                UpdateWrapper<SocialSecurityProvinceCountPO> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("chain_code", po.getChainCode());
                updateWrapper.eq("company_ipo_type", po.getCompanyIpoType());
                updateWrapper.eq("finance_type", po.getFinanceType());
                updateWrapper.eq("province", po.getProvince());
                if (mapper.update(po, updateWrapper) <= 0) {
                    mapper.insert(po);
                }
            } catch (Exception e) {
                log.error("SocialSecurityProvinceCountWriter写入数据失败：" + po, e);
            }
        }
    }
}
